// pages/indexa/index.js
// const request = require('../../utils/request')
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [], // 轮播图
    recommendList: [], // 推荐歌单
    topList: [ // 热歌榜
      {
        name: 'xcv', 
        xxx: [
          {id: 'asdsa'},
          {id: 'dsg'},
          {id: 'sdf'},
          {id: 'fdrg'}
        ]
      },
      {
        name: 'xcv', 
        xxx: [
          {id: 'asdsa'},
          {id: 'dsg'},
          {id: 'sdf'},
          {id: 'fdrg'}
        ]
      },
      {
        name: 'xcv', 
        xxx: [
          {id: 'asdsa'},
          {id: 'dsg'},
          {id: 'sdf'},
          {id: 'fdrg'}
        ]
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // 轮播图
    const bannerData = await request('/banner', {type: 2})
    this.setData({
      banners: bannerData.banners
    })
    console.log(`%c banners成功的回调`, 'color: deeppink', this.data.banners);
    // 推荐歌曲
    const recommendData = await request('/personalized', {limit: 10})
    this.setData({
      recommendList: recommendData.result
    })
    console.log(`%c recommendList成功的回调`, 'color: deeppink', this.data.recommendList);
    // 排行榜 playlist id name tracks
    let index = 0, resData, emptyList = []
    while(index < 5) {
      resData = await request('/top/list', {idx: index})
      emptyList.push({
        id: resData.playlist.id,
        name: resData.playlist.name,
        tracks: resData.playlist.tracks.slice(0, 3)
      })
      index++
      this.setData({
        topList: emptyList
      }, () => {
        console.log('%c topList---', 'color: deeppink', this.data.topList);
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})